package com.nl.lib_core.tool

import android.content.Context
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader

/** 作者: wangfei 创建时间: 2021/9/30 包名: com.nl.lib_core.util 类名: ReadJsonFromAssets */
object ReadLocalDatasUtils {

  /**
   * 得到json文件中的内容
   * @param context
   * @param fileName
   * @return
   */
  fun getJsonFromAssets(context: Context, fileName: String?): String {
    val stringBuilder = StringBuilder()
    // 获得assets资源管理器
    val assetManager = context.assets
    // 使用IO流读取json文件内容
    try {
      val bufferedReader = BufferedReader(InputStreamReader(assetManager.open(fileName!!), "utf-8"))
      var line: String?
      while (bufferedReader.readLine().also { line = it } != null) {
        stringBuilder.append(line)
      }
      bufferedReader.close()
    } catch (e: IOException) {
      e.printStackTrace()
    }
    return stringBuilder.toString()
  }
}
